package com.ndp.fb.scheduler.job.adpublish;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.ec.core.api.DPARobotCoreService;
import com.ndp.fb.constant.ConfigConst;
import com.ndp.fb.enums.publish.PublishStatus;
import com.ndp.fb.exception.CheckedException;
import com.ndp.fb.mongo.api.AdMarketTargetMongoService;
import com.ndp.fb.mongo.model.autocreatead.AdMarketTarget;
import com.ndp.fb.mq.api.service.schedulerservice.AdAccountsSchedulerService;
import com.ndp.fb.mq.api.service.schedulerservice.AdPublishSchedulerService;
import com.ndp.fb.rdb.api.PublishBatchRdbService;
import com.ndp.fb.rdb.model.EntrustAccount;
import com.ndp.fb.rdb.model.PublishBatch;
import com.ndp.fb.scheduler.annotation.CronScheduled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by infi on 2015/1/26.
 */
@Component
public class AdPublishJob {
	private static Logger logger = LoggerFactory.getLogger(AdPublishJob.class);
	@Reference
	AdAccountsSchedulerService adaccountsSchedulerService;
	@Reference
	PublishBatchRdbService publishBatchRdbService;
	@Reference
	private AdPublishSchedulerService adPublishSchedulerService;
	@Reference
	private DPARobotCoreService dPARobotCoreService;
	@Reference
	private AdMarketTargetMongoService adMarketTargetMongoService;
	/**
	 * 获取广告账号下面营销活动和adset数量
	 */
	@CronScheduled(cronkey = ConfigConst.SCHEDULE_ADCAMP_ADSET_COUNT_CRON, enablekey = ConfigConst.SCHEDULE_ADCAMP_ADSET_COUNT_ENABLE)
	public void publishAdCampCountAndAdSet() {
		logger.info("获取广告账号下面营销活动和adset数量");
		List<EntrustAccount> allAdAccount = adaccountsSchedulerService
				.getAdAccountByRoles("ADMIN", "GENERAL_USER", "REPORTS_ONLY");
		if (CollectionUtils.isNotEmpty(allAdAccount)) {
			for (EntrustAccount entrustAccount : allAdAccount) {
				adPublishSchedulerService
						.fetchAdCampCountAndAdSetCount(entrustAccount
								.getFbAccountId());
			}
		}

	}

	/**
	 * 获取自定义受众的数据，每10分钟一次
	 */
	@CronScheduled(cronkey = ConfigConst.SCHEDULE_CUSTOMAUDIENCES_CRON, enablekey = ConfigConst.SCHEDULE_CUSTOMAUDIENCES_ENABLE)
	public void fetchCustomaudiences() {
		List<EntrustAccount> allAdAccount = adaccountsSchedulerService.getAdAccountByRoles("ADMIN", "GENERAL_USER", "REPORTS_ONLY");
		if (CollectionUtils.isNotEmpty(allAdAccount)) {
			for (EntrustAccount entrustAccount : allAdAccount) {
				logger.info("获取账号下的自定义受众:"+entrustAccount.getFbAccountId());
			    try {
					adPublishSchedulerService.fetchCustomaudiences(entrustAccount.getFbAccountId());
				}catch (Exception e){
					logger.info("获取账号下的自定义受众异常:"+entrustAccount.getFbAccountId()+e.getMessage(),e);
				}
			}
		}

	}

	/**
	 * 定时检查广告预发布是否需要执行
	 */
	@CronScheduled(cronkey = ConfigConst.SCHEDULE_PREPROCESS_SCAN_CRON, enablekey = ConfigConst.SCHEDULE_PREPROCESS_SCAN_ENABLE)
	public void prePublishProcessScan(){
		logger.info("scan pre publish process execute");
		List<Long> listids = publishBatchRdbService.findIdsByPublishStatus(PublishStatus.WAIT_PRE_PROCESS.getCode());
		if (CollectionUtils.isNotEmpty(listids)) {
			for (Long batchId : listids) {
				adPublishSchedulerService.prePublishProcessScan(batchId);
			}
		}
	}
	/**
	 * 定时扫描是否需要创建创意
	 */
	@CronScheduled(cronkey = ConfigConst.SCHEDULE_CREATIVE_SCAN_CRON, enablekey = ConfigConst.SCHEDULE_CREATIVE_SCAN_ENABLE)
	public void publishAdCreativeScan() {
		logger.info("定时扫描是否需要创建创意");
		List<PublishBatch> batchList = publishBatchRdbService.findByGoingPublishStatus(PublishStatus.WAIT_PUPLISH.getCode(),
				PublishStatus.REDO.getCode());
		if (CollectionUtils.isNotEmpty(batchList)) {
			for (PublishBatch batch : batchList) {
				adPublishSchedulerService.publishAdCreativeScan(batch.getId());
			}
		}
	}


	/**
	 * 检查创意是否完成
	 */
	@CronScheduled(cronkey = ConfigConst.SCHEDULE_CREATIVE_STATUS_SCAN_CRON, enablekey = ConfigConst.SCHEDULE_CREATIVE_STATUS_SCAN_ENABLE)
	public void publishAdCreativeStatusScan() {
		logger.info("检查创意是否完成");
		List<PublishBatch> batchList = publishBatchRdbService.findByGoingPublishStatus(PublishStatus.PUBLISHING.getCode(),
				PublishStatus.REDO.getCode());
		if (CollectionUtils.isNotEmpty(batchList)) {
			for (PublishBatch batch : batchList) {
				adPublishSchedulerService.publishAdCreativeStatusScan(batch);
			}
		}
		
	}

	/**
	 * 定时扫描广告是否全部创建完成
	 */
	@CronScheduled(cronkey = ConfigConst.SCHEDULE_ADGROUP_STATUS_SCAN_CRON, enablekey = ConfigConst.SCHEDULE_ADGROUP_STATUS_SCAN_ENABLE)
	public void publishAdGroupStatusScan() {
		logger.info("定时扫描广告是否全部创建完成");
		adPublishSchedulerService.publishAdGroupStatusScan();
	}

	/**
	 * 第三方图片自动审核启动
	 */
	@CronScheduled(cronkey = ConfigConst.SCHEDULE_THIRDPIC_PUBLISH_CRON, enablekey = ConfigConst.SCHEDULE_THIRDPIC_PUBLISH_ENABLE)
	public void autoPublishForThirdpic() {
		adPublishSchedulerService.autoPublishForThirdpic();
	}

	/**
	 * 第三方图片的广告实时获取其广告状态 adgroup_status
	 */
	@CronScheduled(cronkey = ConfigConst.SCHEDULE_THIRDPIC_AUDIT_CRON, enablekey = ConfigConst.SCHEDULE_THIRDPIC_AUDIT_ENABLE)
	public void autoAuditAd() {
		logger.info("needFix第三方");
		adPublishSchedulerService.autoAuditAd();
	}

	/**
	 * 按照国家拉去fb的用户数量
	 */
	@CronScheduled(cronkey = ConfigConst.SCHEDULE_FETCH_FB_USERS_CRON, enablekey = ConfigConst.SCHEDULE_FETCH_FB_USERS_ENABLE)
	public void fetchFbUsers() {
		logger.info("fetchFbUsers");
		adPublishSchedulerService.fetchFbUsers();
	}
	
	/**
	 * 扫描营销目标发布是否准备好
	 */
	@CronScheduled(cronkey = ConfigConst.SCHEDULE_FETCH_ADMARKETTARGET_PARAM_CRON, enablekey = ConfigConst.SCHEDULE_FETCH_ADMARKETTARGET_PARAM_ENABLE)
	public void fetchAdMarketTarget() {
		logger.info("prePublishAdMarketTargetScan");
		adPublishSchedulerService.prePublishAdMarketTargetScan();
	}
	
	/**
	 * 扫描营销目标发布是否准备好
	 * @throws CheckedException 
	 */
	@CronScheduled(cronkey = ConfigConst.SCHEDULE_OPTIMIZE_ADMARKETTARGET_CRON, enablekey = ConfigConst.SCHEDULE_OPTIMIZE_ADMARKETTARGET_ENABLE)
	public void optimizeAdMarketTarget() throws CheckedException {
		logger.info("optimizeAdMarketTarget");
		List<AdMarketTarget> adMarketTargets = adMarketTargetMongoService.getAllList();
		for(AdMarketTarget adMarketTarget:adMarketTargets){
			dPARobotCoreService.optimizeMarketingTarget(adMarketTarget.getId(), false);
		}
	}
}
